//
//  CourseDetailTableViewController.swift
//  约健
//
//  Created by tyh on 2017/10/10.
//  Copyright © 2017年 tyh. All rights reserved.
//

import UIKit

class CourseDetailTableViewController: UITableViewController {
    var Course:CourseingModel!
    var turebarbtnItem:UIBarButtonItem!
    var rightBtn:UIButton = UIButton.init()
    override func viewDidLoad() {
        super.viewDidLoad()
        automaticallyAdjustsScrollViewInsets = false
        self.title = Course.name as String
        rightBtn.sizeToFit()
        rightBtn.addTarget(self, action: #selector(Ifcollect), for: UIControlEvents.touchUpInside)
        turebarbtnItem = UIBarButtonItem.init(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = turebarbtnItem
        self.rightBtn.setImage(UIImage.init(named: "buzai.png"), for: .normal)
        CreatFootView()

        print(Course.id)
        
        

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 4
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var basecell = UITableViewCell()
        if indexPath.section == 0{
        let cell = DetailHeadTableViewCell.creatCell(tableView)
        let imageStr = "http://172.24.10.175/workout/Uploads/" + (Course.thumb! as String)
        let imageUrl = URL(string: imageStr)
        let data = try! Data(contentsOf: imageUrl!)
        let image = UIImage(data: data)
        cell.Pic.image = image
        
        var type:String!
        if Course.degree == "1"{
            type = "入门"
        }else if Course.degree == "2"{
            type = "菜鸟"
        }else if Course.degree == "3"{
            type = "进阶"
        }
        cell.Address.text = type
            basecell = cell
        }else if indexPath.section == 1{
            let cell:TextTableViewCell = tableView.dequeueReusableCell(withIdentifier: "textcell", for: indexPath) as! TextTableViewCell
            
            cell.TextName.text = "训练简介"
            cell.TextContent.text = Course.intro as String
            basecell = cell
        }else if indexPath.section == 2{
            let cell:TextTableViewCell = tableView.dequeueReusableCell(withIdentifier: "textcell", for: indexPath) as! TextTableViewCell
            
            cell.TextName.text = "最新消息"
            cell.TextContent.text = Course.notice as String
            basecell = cell

        }
        
        else if indexPath.section == 3{
            let cell = AttentionTableViewCell.creatCell(tableView)
            
            cell.note1.text = "价格"
            cell.note2.text = "训练时间"
            cell.note3.text = "联系电话"
            
            cell.TakeText.text = Course.price as String
            cell.Time.text = Course.traintime as String
            cell.Tele.text = Course.tel as String
            basecell = cell
        }
        
        
        basecell.selectionStyle = UITableViewCellSelectionStyle.none
        return basecell
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0{
        return height * 0.3
        }else if indexPath.section == 1 || indexPath.section == 2 {
            return height * 0.1
        }else {
            return height * 0.15
        }
    }

 override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
 
 return 8
 
 }
    
    func Ifcollect(){
 
    }
 
    func CreatFootView(){
        let footerView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: 60))
        let footButton:UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: width, height: 60))
        footButton.setTitle("加入训练", for: .normal)
        footButton.setTitleColor(UIColor.white, for: .normal)
        footButton.backgroundColor = UIColor.orange
        footButton.addTarget(self, action: #selector(AddCourse), for: .touchUpInside)
        footerView.addSubview(footButton)
        tableView.tableFooterView = footerView
        
    }
    func AddCourse(){
        
    }


    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
